package com.kkb.robotcustomerservice.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.kkb.robotcustomerservice.dao.ChatDao;
import com.kkb.robotcustomerservice.dao.ChatMapper;
import com.kkb.robotcustomerservice.entities.Chat;
import com.kkb.robotcustomerservice.util.JsonResult;
import com.kkb.robotcustomerservice.service.ChatService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author huangdong50
 * @version V1.0
 * @Package com.kkb.robotcustomerservice.service.impl
 * @date 2020/9/4 0004 22:04
 */
@Service
public class ChatServiceImpl implements ChatService {

    @Autowired
    private ChatMapper mapper;

    @Autowired(required = false)
    private ChatDao chatDao;

    @Override
    public int add(Chat chat) {
        return chatDao.insert(chat);
    }


    @Override
    public JsonResult<PageInfo<Chat>> jsonResult(Integer userId, int chatId, int pageNum, int pageSize) {

        JsonResult<PageInfo<Chat>> result = new JsonResult<>();
        PageInfo<Chat> pageInfo = PageHelper.startPage(pageNum, pageSize).doSelectPageInfo(() -> {
            //按消息ID倒序分页查询
            List<Chat> chats = mapper.selectList(Wrappers.<Chat>lambdaQuery().eq(Chat::getValidState, "1").lt(Chat::getChatId,chatId).orderByDesc(Chat::getChatId));
            result.setSuccess(chats.size() > 0);
            result.setMsg(chats.size() > 0 ? "查询成功" : "查询失败");


        });
        //数据中返回分页对象
        result.setData(pageInfo);
        return result;
    }
}
